<!--
  * OCR配置检查页面
  *
  * @Author:     SmartAdmin
  * @Date:       2024-01-01
  * @Wechat:     1024创新实验室
  * @Email:      1024lab@163.com
  * @Copyright:  1024创新实验室
-->
<template>
  <div class="ocr-config-page">
    <a-card title="OCR配置检查" :bordered="false">
      <a-space direction="vertical" size="large" style="width: 100%">
        <!-- 配置信息 -->
        <a-card title="当前配置" size="small">
          <a-descriptions :column="2" bordered>
            <a-descriptions-item label="Tesseract数据路径">
              {{ configInfo.tessdataPath || '未配置' }}
            </a-descriptions-item>
            <a-descriptions-item label="文件上传大小限制">
              {{ configInfo.uploadMaxFileSize || 50 }} MB
            </a-descriptions-item>
            <a-descriptions-item label="请求大小限制">
              {{ configInfo.uploadMaxRequestSize || 50 }} MB
            </a-descriptions-item>
            <a-descriptions-item label="OCR线程池大小">
              {{ configInfo.threadPoolSize || 4 }}
            </a-descriptions-item>
            <a-descriptions-item label="图片DPI">
              {{ configInfo.imageDpi || 300 }}
            </a-descriptions-item>
            <a-descriptions-item label="启用图片预处理">
              <a-tag :color="configInfo.enableImagePreprocessing ? 'green' : 'red'">
                {{ configInfo.enableImagePreprocessing ? '是' : '否' }}
              </a-tag>
            </a-descriptions-item>
            <a-descriptions-item label="启用多语言识别">
              <a-tag :color="configInfo.enableMultiLanguage ? 'green' : 'red'">
                {{ configInfo.enableMultiLanguage ? '是' : '否' }}
              </a-tag>
            </a-descriptions-item>
            <a-descriptions-item label="启用LSTM引擎">
              <a-tag :color="configInfo.enableLstmEngine ? 'green' : 'red'">
                {{ configInfo.enableLstmEngine ? '是' : '否' }}
              </a-tag>
            </a-descriptions-item>
            <a-descriptions-item label="对比度增强系数">
              {{ configInfo.contrastEnhancement || 1.2 }}
            </a-descriptions-item>
            <a-descriptions-item label="页面分割模式">
              {{ configInfo.pageSegMode || 1 }}
            </a-descriptions-item>
          </a-descriptions>
        </a-card>

        <!-- 状态检查 -->
        <a-card title="OCR状态检查" size="small">
          <a-space direction="vertical" style="width: 100%">
            <a-button type="primary" @click="checkOcrConfig" :loading="checking">
              检查OCR配置
            </a-button>
            
            <div v-if="ocrStatus">
              <a-alert
                :type="ocrStatus.available ? 'success' : 'error'"
                :message="ocrStatus.available ? 'OCR配置正常' : 'OCR配置异常'"
                show-icon
              />
              
              <a-divider />
              
              <a-descriptions :column="1" bordered>
                <a-descriptions-item label="可用性">
                  <a-tag :color="ocrStatus.available ? 'green' : 'red'">
                    {{ ocrStatus.available ? '可用' : '不可用' }}
                  </a-tag>
                </a-descriptions-item>
                <a-descriptions-item label="默认语言">
                  {{ ocrStatus.defaultLanguage || '无' }}
                </a-descriptions-item>
                <a-descriptions-item label="可用语言">
                  <a-space wrap>
                    <a-tag v-for="lang in ocrStatus.availableLanguages" :key="lang" color="blue">
                      {{ lang }}
                    </a-tag>
                  </a-space>
                </a-descriptions-item>
              </a-descriptions>
              
              <!-- 错误信息 -->
              <div v-if="ocrStatus.errors && ocrStatus.errors.length > 0">
                <a-divider />
                <a-alert
                  type="error"
                  message="错误信息"
                  description=""
                />
                <a-list size="small" bordered>
                  <a-list-item v-for="(error, index) in ocrStatus.errors" :key="index">
                    {{ error }}
                  </a-list-item>
                </a-list>
              </div>
              
              <!-- 信息 -->
              <div v-if="ocrStatus.infos && ocrStatus.infos.length > 0">
                <a-divider />
                <a-alert
                  type="info"
                  message="信息"
                  description=""
                />
                <a-list size="small" bordered>
                  <a-list-item v-for="(info, index) in ocrStatus.infos" :key="index">
                    {{ info }}
                  </a-list-item>
                </a-list>
              </div>
            </div>
          </a-space>
        </a-card>

        <!-- 配置说明 -->
        <a-card title="配置说明" size="small">
          <a-alert
            type="warning"
            message="OCR配置要求"
            description=""
          />
          <a-list size="small">
            <a-list-item>
              1. 需要安装Tesseract OCR引擎
            </a-list-item>
            <a-list-item>
              2. 下载对应的语言包文件(.traineddata)到tessdata目录
            </a-list-item>
            <a-list-item>
              3. 中文识别需要下载chi_sim.traineddata或chi_tra.traineddata
            </a-list-item>
            <a-list-item>
              4. 英文识别需要下载eng.traineddata
            </a-list-item>
            <a-list-item>
              5. 确保tessdata目录路径配置正确
            </a-list-item>
          </a-list>
          
          <a-divider />
          
          <a-alert
            type="info"
            message="下载地址"
            description=""
          />
          <a-list size="small">
            <a-list-item>
              <a href="https://github.com/tesseract-ocr/tessdata" target="_blank">
                Tesseract语言包下载地址
              </a>
            </a-list-item>
            <a-list-item>
              <a href="https://github.com/tesseract-ocr/tesseract" target="_blank">
                Tesseract OCR安装指南
              </a>
            </a-list-item>
          </a-list>
        </a-card>
      </a-space>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { pdfApi } from '/@/api/business/pdf/pdf-api';

// 响应式数据
const configInfo = ref({});
const ocrStatus = ref(null);
const checking = ref(false);

// 获取配置信息
const getConfigInfo = async () => {
  try {
    const response = await pdfApi.getOcrConfig();
    configInfo.value = response;
  } catch (error) {
    console.error('获取配置信息失败:', error);
  }
};

// 检查OCR配置
const checkOcrConfig = async () => {
  checking.value = true;
  try {
    const response = await pdfApi.checkOcrConfig();
    ocrStatus.value = response;
  } catch (error) {
    console.error('检查OCR配置失败:', error);
    ocrStatus.value = {
      available: false,
      errors: ['检查失败: ' + error.message]
    };
  } finally {
    checking.value = false;
  }
};

// 页面加载时获取配置信息
onMounted(() => {
  getConfigInfo();
});
</script>

<style scoped>
.ocr-config-page {
  padding: 20px;
}
</style> 